package com.mengma.ec.service.impl.portal;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.dao.ProductMapper;
import com.mengma.ec.entity.Product;
import com.mengma.ec.service.portal.IProductService;
import com.mengma.ec.vo.ProductListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xixi
 * @Date: 2018/9/19 09:28
 * @Description:
 */
@Service("iProductService")
public class ProductServiceImpl implements IProductService{

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ServerResponse saveOrUpdata(Product product) {
        //逻辑1.1 设置product 主图字段
        if (StringUtils.isNotBlank(product.getSubImages())){
            String[] subImageArray = product.getSubImages().split(",");
            product.setMainImage(subImageArray[0]);
        }


        //逻辑1.2 判断是新增还是修改
        if (product.getId() == null){//新增操作
            int i = productMapper.insertSelective(product);
            if(i>0){
                return ServerResponse.createBySuccessMessage("新增商品成功");
            }
                return ServerResponse.createByErrorMessage("新增更新商品失败");

        }else{//修改操作
            int i = productMapper.updateByPrimaryKey(product);
            if(i>0){
                return ServerResponse.createBySuccessMessage("更新商品成功");
            }
                return ServerResponse.createByErrorMessage("更新商品失败");
        }
    }
    //1.产品list
    @Override
    public ServerResponse FindAllListData(Integer pageNum, Integer pageSize, HttpSession session) {

        PageHelper.startPage(pageNum,pageSize);
        //获取原始数据源
        List<Product> list = productMapper.findList();

        //转换原始数据源为要展示的vo
        List<ProductListVo> voList = new ArrayList<>();

        for(Product product : list){
            ProductListVo productListVo = convertPVO(product);
            voList.add(productListVo);
        }
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setList(voList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ProductListVo convertPVO(Product product){
        ProductListVo productListVo = new ProductListVo();
        productListVo.setId(product.getId());
        productListVo.setName(product.getName());
        productListVo.setCategoryId(product.getCategoryId());
        productListVo.setMainImage(product.getMainImage());
        productListVo.setPrice(product.getPrice());
        productListVo.setSubtitle(product.getSubtitle());
        productListVo.setStatus(product.getStatus());
        //productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix", "http://img.happymmall.com/"));
        return productListVo;
    }

    //2.产品搜索
    @Override
    public ServerResponse<PageInfo> search(String productName, Integer productId, Integer pageNum, Integer pageSize, HttpSession session) {
        PageHelper.startPage(pageNum,pageSize);
        //获取原始数据源
        List<Product> list = productMapper.findsearch(productName,productId);

        //转换原始数据源为要展示的vo
        List<ProductListVo> voList = new ArrayList<>();

        for(Product product : list){
            ProductListVo productListVo = convertPVO(product);
            voList.add(productListVo);
        }
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setList(voList);
        return ServerResponse.createBySuccess(pageInfo);
    }


}
